package app.displayTree.special.sub
{
    import app.debug.APPLog;
    import app.displayTree.analyze.DisplayTreeAnalyzeSupport;
    import app.utils.MetaInfoUtils;
    import app.utils.SWFInfoTreeImageUtils;

    import copyengine.infoTree.meta.CES9ScaleImageMeta;
    import copyengine.infoTree.meta.CESDisplayObjectMeta;
    import copyengine.infoTree.meta.CESRectangleMeta;
    import copyengine.utils.CEStringUtils;

    import flash.display.Bitmap;
    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;
    import flash.display.Sprite;
    import flash.geom.Rectangle;

    /**
     * 当一个元件导出类以S9_或者s9_开头时候则该元件当做S9元件处理
     * 比如一个Mc导出类叫做S9_RedPanelBg 则该元件会被当做S9类型元件处理 且
     * 后续碰到Library中以RedPanelBg_开头的元件 都会被该Handle类处理
     * 比如RedPanelBg_A1 RedPanelBg_A2 等等
     *
     * 每一个S9元件内部
     * 1` 应该包含且仅包含两个元件 一个元件名字为mc作为需要S9的元件
     *    另外一个元件名字middle命名用于中间区域Rect区域的选择
     *
     * 2` 元件均以(0,0)对齐 因为计算mS9MiddleAreaRect是以00点计算的
     *
     */
    public class S9ScaleSpecialSymbolHandle extends SpecialSymbolHandleBasic
    {

        //当前S3元件的前缀名
        private var mS9SymbolNamePR:String;

        //S9元件的位图
        private var mS9SymbolBitmap:Bitmap;

        //S9中间会Scale的区域的矩形范围
        private var mS9MiddleAreaRect:CESRectangleMeta;

        public function S9ScaleSpecialSymbolHandle()
        {
        }

        override protected function initialize(_symbolKey:String, _symbol:DisplayObject):void
        {
            mS9SymbolNamePR = CEStringUtils.afterFirst(_symbolKey, "_");

            var s9Symbol:DisplayObject = (_symbol as DisplayObjectContainer).getChildByName("mc");
            var s9MiddleMc:DisplayObject = (_symbol as DisplayObjectContainer).getChildByName("middle");
            if (s9Symbol == null || s9MiddleMc == null) {
                APPLog.err("S9 symbol : " + _symbolKey + " not contain child named with `mc` as target or not contain `middle` as reference object ");
                return;
            }

            mS9MiddleAreaRect = new CESRectangleMeta();
            MetaInfoUtils.fillRectangleMetaInfo(mS9MiddleAreaRect, s9MiddleMc.getRect(s9Symbol))

            mS9SymbolBitmap = new Bitmap(SWFInfoTreeImageUtils.cacheDisplayObjectToBitmapData(s9Symbol));
        }

        override protected function isCanHandleThisSpecialSymbolKey(_symbolKey:String):Boolean
        {
            return CEStringUtils.beginsWith(_symbolKey, "S9_") || CEStringUtils.beginsWith(_symbolKey, "s9_");
        }

        override protected function isSpecialSymbol(_symbol:Sprite):Boolean
        {
            //按注释中的例子来说的话此时mS9SymbolNamePR = RedPanelBg,而需要beginsWith的Key
            //应该为RedPanelBg_ 这样防止RedPanelBgXXX这样的名字被误操作
            return CEStringUtils.beginsWith(getMcAs3ExportName(_symbol), mS9SymbolNamePR + "_");
        }

        override protected function doGenerateSpecialSymbolMeta(_symbol:Sprite, _support:DisplayTreeAnalyzeSupport):CESDisplayObjectMeta
        {
            var scale9Meta:CES9ScaleImageMeta = new CES9ScaleImageMeta();
            MetaInfoUtils.fillDisplayObjectMetaBasicInfo(_symbol, scale9Meta);

            /**
             * S3 S9的Meta需要重新修正宽高
             *
             * 因为S3和S9不像普通的Mc那样 当宽高变换时候为Scale调整, (S3,S9的Scale恒定为1)
             * 所以当在FLA中对S3.S9元件进行旋转操作时候,如果直接通过fillDisplayObjectMetaBasicInfo函数读取的宽高(其实读取的是矩阵) 得到的宽高
             * 是 _symbol.getBounds(_symbol.parent)的结果,  比如mc的高度是10,当期Rotation不为0时候,读到的高度不为10(可以查看Flash的属性面板 里面的高度会变)
             */
            var bounds:Rectangle = _symbol.getBounds(_symbol);
            scale9Meta.width = bounds.width;
            scale9Meta.height = bounds.height;

            scale9Meta.textureMeta = _support.pushMcToExportAndReturnMcTextureInfo(mS9SymbolBitmap);
            scale9Meta.middleAreaRect = mS9MiddleAreaRect;

            return scale9Meta;
        }

    }
}
